package com.xuecheng.media.controller;

import com.aliyuncs.exceptions.ClientException;
import com.xuecheng.commons.model.dto.MediaDto;
import com.xuecheng.commons.model.dto.MediaQueryDto;
import com.xuecheng.commons.model.dto.PageRequest;
import com.xuecheng.commons.model.dto.VodUploadDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.model.vo.VodUploadVo;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.media.domain.po.Media;
import com.xuecheng.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
public class MediaController {
    @Autowired
    private MediaService mediaService;

    /*
    * 获取上传凭证
    * */
    @PostMapping("/media/vod-token")
    public ResponseResult<VodUploadVo> vodToken(@RequestBody VodUploadDto dto){
        return mediaService.vodToken(dto);
    }

    //刷新上传凭证
    @GetMapping("/media/refresh-vod-token/{videoId}")
    public ResponseResult<VodUploadVo> refreshVodToken(@PathVariable("videoId") String videoId){
        return mediaService.refreshVodToken(videoId);
    }

    /*
    * 保存媒资
    * */
    @PostMapping("/media")
    public ResponseResult saveMedia(@RequestBody MediaDto dto) throws ClientException {
        return mediaService.saveMedia(dto);
    }

    /*
    * 媒资分页查询
    * */
    @PostMapping("/media/list")
    public ResponseResult<MediaVo> findByPage(PageRequest pageRequest, @RequestBody MediaQueryDto dto){
        return mediaService.findByPage(pageRequest,dto);
    }

    /*
    * 媒资预览
    * */
    @GetMapping("/media/preview/{mediaId}")
    public ResponseResult<String> preview(@PathVariable("mediaId") Long mediaId) throws ClientException {
        return mediaService.preview(mediaId);
    }

    /*
    * 媒资删除
    * */
    @DeleteMapping("/media/{mediaId}")
    public ResponseResult deleteMedia(@PathVariable("mediaId") Long mediaId) throws ClientException {
        return mediaService.deleteMedia(mediaId);
    }

    /*
    * 根据id插叙媒资对象
    * */
    @GetMapping("/media/{mediaId}")
    public ResponseResult<MediaVo> findById(@PathVariable("mediaId") Long mediaId){
        //2.调用service查询
        Media media = mediaService.getById(mediaId);
        //3.转vo
        MediaVo mediaVo = BeanHelper.copyProperties(media, MediaVo.class);
        //4.返回结果
        return ResponseResult.okResult(mediaVo);
    }

}
